/**
 * 
 */
package cn.sundat.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;

import cn.sundat.model.FriendLink;
import cn.sundat.model.SysCfg;

/**
 * @author XIN
 *
 */
public class FriendLinkDaoImpl extends HibernateDaoSupport implements FriendLinkDao {

	public void addFriendLink(FriendLink friendLink) {

		getHibernateTemplate().save(friendLink);
	}

	@SuppressWarnings("unchecked")
	public List<FriendLink> findFriendLinkByPage(int nowPage, int pageSize) {
		// TODO Auto-generated method stub
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(FriendLink.class);
		detachedCriteria.addOrder(Order.desc("id"));
		List<FriendLink> findByCriteria = (List<FriendLink>) getHibernateTemplate().findByCriteria(detachedCriteria,
				(nowPage - 1) * pageSize, pageSize);
		return findByCriteria;
	}

	public int findCount() {
		return getHibernateTemplate().findByCriteria(DetachedCriteria.forClass(FriendLink.class)).size();
	}

	@SuppressWarnings("unchecked")
	public List<FriendLink> findFriendLinkBykey(String keyWords) {
		// TODO Auto-generated method stub
		DetachedCriteria forClass = DetachedCriteria.forClass(FriendLink.class);
		forClass.add(Restrictions.like("sitename", keyWords, MatchMode.ANYWHERE));
		List<FriendLink> findByCriteria = (List<FriendLink>) getHibernateTemplate().findByCriteria(forClass);
		return findByCriteria;
	}

	public boolean delBatch(String batchList) {
		Session session = getSessionFactory().openSession();
		int result = session.createQuery("delete from FriendLink WHERE id IN ("+batchList+")").executeUpdate();
		return result > 0 ? true : false;

	}

	public void editFriendLink(FriendLink friendLink) {
		// TODO Auto-generated method stub
		getHibernateTemplate().update(friendLink);
		
	}

	public List<FriendLink> findAllFriendLink() {
		// TODO Auto-generated method stub
		@SuppressWarnings("unchecked")
		List<FriendLink> findAllFriendLinkList = (List<FriendLink>) getHibernateTemplate().findByCriteria(DetachedCriteria.forClass(FriendLink.class));
		return findAllFriendLinkList;
	}

	@SuppressWarnings("unchecked")
	public List<FriendLink> findFriendLinkById(int id) {
		// TODO Auto-generated method stub
		return (List<FriendLink>) getHibernateTemplate().find("from FriendLink where id=?", id);
	}

	@SuppressWarnings("unchecked")
	public List<SysCfg> findAllInfo() {
		// TODO Auto-generated method stub
		return (List<SysCfg>) getHibernateTemplate().findByCriteria(DetachedCriteria.forClass(SysCfg.class));
	}

	public void editSysInfo(SysCfg sysCfg) {
		// TODO Auto-generated method stub
		getHibernateTemplate().update(sysCfg);
		
	}

}
